package com.dlc.felear.xiaoensale.dao;

import android.content.Context;
import android.util.Log;

import com.dlc.felear.xiaoensale.entity.SfPosition;
import com.dlc.felear.xiaoensale.helper.JsonHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SfPositionDao {
    private static final String TAG="SfPositionDao";
    private Dao<SfPosition, Integer> PipeInfoDao;

    /**
     * 构造方法
     * 获得数据库帮助类实例，通过传入Class对象得到相应的Dao
     *
     * @param context
     */
    public SfPositionDao(Context context) {
        try {
            PipeInfoDao = DetailDataOpenHelper.getInstance(context).getDao(SfPosition.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加一条记录
     *
     * @param PipeInfo
     */
    public void add(SfPosition PipeInfo) {
        try {
            PipeInfoDao.createOrUpdate(PipeInfo);
            Log.e(TAG,"SfPosition:"+ JsonHelper.toJson(PipeInfo));
        } catch (SQLException e) {
            Log.e(TAG,e.getMessage());
            e.printStackTrace();
        }
    }



    /**
     * 删除一条记录
     *
     * @param PipeInfo
     */
    public void delete(SfPosition PipeInfo) {
        try {
            PipeInfoDao.delete(PipeInfo);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }



    /**
     * 查询一条记录
     *
     * @param id
     * @return
     */
    public SfPosition queryForId(int id) {
        SfPosition PipeInfo = null;
        try {
            PipeInfo = PipeInfoDao.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return PipeInfo;
    }


    /**
     * 查询所有记录
     *
     * @return
     */
    public List<SfPosition> queryForAll() {
        List<SfPosition> PipeInfos = new ArrayList();
        try {
            PipeInfos = PipeInfoDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return PipeInfos;
    }

    /**
     * 查询所有记录
     *
     * @return
     */
    public List<SfPosition> queryForAllByMID(String action) {
        List<SfPosition> PipeInfos = new ArrayList();
        try {
            PipeInfos = PipeInfoDao.queryBuilder().where().eq("m_id", action).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return PipeInfos;
    }

    public void deleteByMid(String mid) {
        try {
            DeleteBuilder deleteBuilder = PipeInfoDao.deleteBuilder();
            deleteBuilder.where().eq("m_id", mid);
            deleteBuilder.delete();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
